package zhcms.admin;

import java.io.UnsupportedEncodingException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import zhcms.db.DBManager;
import zhcms.db.DbConn;
import zhcms.db.ConnectionPool.PooledConnection;
import zhcms.framework.Action;
import zhcms.framework.ActionForward;
import zhcms.framework.GyReplace;
import zhcms.framework.GySplit;
import zhcms.tool.GyPage;

public class GyPlus implements Action{

	String pageStr="";
	public GyPlus() {
		
	}
	
	@Override
	public ActionForward excute(HttpServletRequest request,
			HttpServletResponse response) {
		return null;
	}
	
	public int listcount(){
		ResultSet rs = null;
		PooledConnection conn = null;
		String sql="select count(*) as id from gyhy_plus";
		int countlyb=0;
		try {
			conn = DBManager.getConnection();
			rs = conn.executeQuery(sql);
			if (rs.next())
			{
				countlyb=rs.getInt("id");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally {
            conn.close();
		}
		return countlyb;
	}
	
	public ArrayList<HashMap<String, String>> getAllList(int currentpage)
	{
		int listnumcount=this.listcount();
		GyPage gyp=new GyPage();
		gyp.setP(15, listnumcount, "", "", currentpage);
		String limitStr=gyp.limit();
		pageStr=gyp.outPut();
		String sql="select `id` ,`name` ,  `description` ,  `jarclasstruepath` ,  `jarurl` ,  `classname` ,  if(`ismanager`='1','有','无') as `ismanager`,  if(`managerurl` is null,'',`managerurl`) as `managerurl` , if(`managername` is null ,'',`managername`) as `managername`,  if(`isbaohan`='1','有','无') as  `isbaohan`,  if(`isok`='1','启用','未启用') as  `isok`,  `author` ,  `addtime` from gyhy_plus order by id desc limit "+limitStr;
		return DbConn.executeQuery(sql);
	}
	
	public ArrayList<HashMap<String, String>> getOneList(int postid)
	{
		String sql="select `id` ,`name` ,  `description` ,  `jarclasstruepath` ,  `jarurl` ,  `classname` ,  `ismanager` ,  `managerurl` , `managername` ,  `isbaohan` ,  `isok` ,  `author` ,  `addtime`  from gyhy_plus where id='"+postid+"'";
		return DbConn.executeQuery(sql);
	}
	
	public ArrayList<HashMap<String, String>> getOneList(String postid)
	{
		String sql="select `id` ,`name` ,  `description` ,  `jarclasstruepath` ,  `jarurl` ,  `classname` ,  `ismanager` ,  `managerurl` , `managername` ,  `isbaohan` ,  `isok` ,  `author` ,  `addtime`  from gyhy_plus where id='"+postid+"'";
		return DbConn.executeQuery(sql);
	}
	
	public ArrayList<HashMap<String, String>> getManagerList()
	{
		String sql="select `id` ,`name` ,  `description` ,  `jarclasstruepath` ,  `jarurl` ,  `classname` ,  `ismanager` ,  `managerurl` , `managername` ,  `isbaohan` ,  `isok` ,  `author` ,  `addtime`  from gyhy_plus where ismanager='1' and isok='1'";
		return DbConn.executeQuery(sql);
	}
	
	public String getPageStr() {
		return pageStr;
	}

	public void setPageStr(String pageStr) {
		this.pageStr = pageStr;
	}
	
	private String readPath()
	{
		String filePath = this.getClass().getResource("").getPath().toString();
		//System.out.println(filePath);
		
		if (filePath.indexOf("WEB-INF") > 0) {
			GySplit b = new GySplit();
			b.tool(filePath, "/WEB-INF");
			String[] temp_array = b.Show();
			b = null;
			filePath = temp_array[0];
		}

		filePath = filePath + "/";
		
		//处理带file：//这种情况
		GyReplace r = new GyReplace();
		filePath = r.replace(filePath, "file:", "");
		r = null;
		
		return filePath;
	}
	
	public ActionForward save(HttpServletRequest request, HttpServletResponse response)
	{
		try {
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("utf-8");
		} 
		catch (UnsupportedEncodingException e1) 
		{
			
		}
		String postid=getV(request.getParameter("postid"));
		//System.out.println("postid+++++++++++++"+postid);
		String name=getV(request.getParameter("name"));
		
		String description=getV(request.getParameter("description"));
		
		String jarclasstruepath=this.readPath()+"WEB-INF/lib/";
		
		String jarurl=getV(request.getParameter("jarurl"));
		
		String classname=getV(request.getParameter("classname"));
		
		String ismanager=getV(request.getParameter("ismanager"));

		String managerurl=getV(request.getParameter("managerurl"));
		
		String managername=getV(request.getParameter("managername"));
		
		String isbaohan=getV(request.getParameter("isbaohan"));
		
		String isok1=getV(request.getParameter("isok"));
		
		String author=getV(request.getParameter("author"));
		
		
		
		PooledConnection conn = null;
		String sqlStr1="";
		
		if(postid==null || postid.length()<=0 || postid.equals("null"))
		{
			sqlStr1="insert into `gyhy_plus` (   `name` ,  `description` ,  `jarclasstruepath` ,  `jarurl` ,  `classname` ,  `ismanager` ,  `managerurl`,`managername` ,  `isbaohan` ,  `isok` ,  `author` ,  `addtime` ) values('"+name+"','"+description+"','"+jarclasstruepath+"','"+jarurl+"','"+classname+"','"+ismanager+"','"+managerurl+"','"+managername+"','"+isbaohan+"','"+isok1+"','"+author+"',now());";
		}
		else
		{
			sqlStr1="update `gyhy_plus` set `name`='"+name+"' , `description`='"+description+"' , `jarclasstruepath`='"+jarclasstruepath+"' , `jarurl`='"+jarurl+"' , `classname`='"+classname+"' , `ismanager`='"+ismanager+"' , `managerurl`='"+managerurl+"' , `managername`='"+managername+"' , `isbaohan`='"+isbaohan+"' , `isok`='"+isok1+"' , `author`='"+author+"' where `id`="+postid;
		}
		
		//System.out.println(sqlStr1);
		boolean isok=false;
		try {
			conn = DBManager.getConnection();
			conn.executeUpdate(sqlStr1);
			conn.close();
			isok=true;
		} catch (SQLException e) {
			isok=false;
			e.printStackTrace();
		}
		finally {
     conn.close();
		}
		
		if(isok==true)
		{
			return new ActionForward("/admin/admin_plus.jsp",3,"更新成功");
		}
		else
		{
			return new ActionForward("/admin/admin_plus.jsp",3,"更新失败");
		}
	}
	
	public ActionForward del(HttpServletRequest request, HttpServletResponse response)
	{
		
		try {
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("utf-8");
		} 
		catch (UnsupportedEncodingException e1) 
		{
			
		}
		String postid=getV(request.getParameter("postid"));
		//System.out.println("postid+++++++++++++"+postid);
		
		PooledConnection conn = null;
		String sqlStr1="";
		
		sqlStr1="delete from  `gyhy_plus` where `id`="+postid;
		
		//System.out.println(sqlStr1);
		boolean isok=false;
		try {
			conn = DBManager.getConnection();
			conn.executeUpdate(sqlStr1);
			conn.close();
			isok=true;
		} catch (SQLException e) {
			isok=false;
			e.printStackTrace();
		}
		finally {
            conn.close();
		}
		if(isok==true)
		{
			return new ActionForward("/admin/admin_plus.jsp",3,"更新成功");
		}
		else
		{
			return new ActionForward("/admin/admin_plus.jsp",3,"更新失败");
		}
	}

	public static String TransactSQLInjection(String str)
	{
	    return str.replaceAll(".*([';]+|(--)+).*", " ");
	}

	private String getV(String S)
	{
		//System.out.println(S);
		String returnS;
		if(S==null || S.length()<=0)
		{
			returnS="";
		}
		else
		{
			returnS=S;
		}
		return TransactSQLInjection(returnS);
	}

}
